package mq

import (
	"fmt"
	"gitee.com/DonHz/cloud-file-storage/config"
	"github.com/streadway/amqp"
	"log"
)

var (
	done chan bool
)

func StartConsume(qName, cName string, callback func(msg []byte) bool) {
	// 1.通过channel Consume获取消息通道
	fmt.Println("channel:", channel)
	conn, err := amqp.Dial(config.RabbitURL)
	channel, err = conn.Channel()
	fmt.Println("channel:", channel)
	msgs, err := channel.Consume(qName, cName, true, false, false, false, nil)
	if err != nil {
		log.Println(err)
		return
	}
	// 2.循环获取队列消息
	done = make(chan bool)
	go func() {
		for msg := range msgs {
			processSuc := callback(msg.Body)
			if !processSuc {
				//TODO:将任务写道另一个队列，用于异常情况重试
			}
		}
	}()

	<-done

	channel.Close()
}
